﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobsBoardDAL.DTOs;
using JobsBoardDAL;
using System.Windows;
using JobBoardAdminPanel.Commands;

namespace JobBoardAdminPanel.ViewModels
{
    public class CountryViewModel : ViewModelBase
    {
        private CountryDTO country;

        public CountryViewModel(CountryDTO country)
        {
            this.country = country;

        }

        public CountryDTO Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }

        public int Id
        {
            get
            {
                return this.country.ID;
            }
        }

        public string CountryName
        {
            get
            {
                return this.country.CountryName;
            }
            set
            {
                if (this.country.CountryName != value)
                {
                    this.country.CountryName = value;
                    OnPropertyChanged("CountryName");
                }
            }
        }

        internal void Update()
        {
            if (String.IsNullOrEmpty(CountryName) || CountryName.Length > 50)
            {
                MessageBox.Show("Invalid name.");
                return;
            }

            try
            {
                JobsBoardCRUD.EditCountry(this.country);
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }

        }

        internal void Delete()
        {
            try
            {
                JobsBoardCRUD.DeleteCountry(this.country);
            }
            catch
            {
                MessageBox.Show("Update unseccesful. Other objects are using thes Country.");
                return;
            }
        }

        internal void Add()
        {
            if (String.IsNullOrEmpty(CountryName) || CountryName.Length > 50)
            {
                MessageBox.Show("Invalid name.");
                return;
            }

            try
            {
                JobsBoardCRUD.AddCountry(this.country);
                this.CountryName = null;
            }
            catch
            {
                MessageBox.Show("Update unseccesful");
                return;
            }

            
        }
    }
}
